package com.iotechn.unimall.admin.api.seed;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.LeaseSeedDO;
import com.iotechn.unimall.data.domain.SeedDO;
import com.iotechn.unimall.data.mapper.LeaseSeedMapper;
import com.iotechn.unimall.data.mapper.SeedMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;


@Service
public class AdminSeedServiceImpl implements AdminSeedService {

    @Autowired
    private SeedMapper seedMapper;
    @Autowired
    private LeaseSeedMapper leaseSeedMapper;

    @Override
    public Page<SeedDO> list(Integer page, Integer limit, String name, Long adminId) throws ServiceException {
        Wrapper<SeedDO> wrapper = new EntityWrapper<SeedDO>();
        if (!StringUtils.isEmpty(name))
            wrapper.like("name", name);
        wrapper.orderBy("id", false);
        Integer count = seedMapper.selectCount(wrapper);
        List<SeedDO> SeedDOS = seedMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(SeedDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SeedDO create(SeedDO seedDO, Long adminId) throws ServiceException {
        Integer count = seedMapper.selectCount(new EntityWrapper<SeedDO>().eq("name", seedDO.getName()));
        if(count > 0)
            throw new AdminServiceException(ExceptionDefinition.LEASE_SEED_CANNOT_REPEATED);
        if (seedMapper.insert(seedDO) > 0) {
            seedDO.setId(seedDO.getId());
            return seedDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(SeedDO seedDO, Long adminId) throws ServiceException {
        Integer count = seedMapper.selectCount(new EntityWrapper<SeedDO>().eq("name", seedDO.getName())
                .ne("id",seedDO.getId()));
        if(count > 0)
            throw new AdminServiceException(ExceptionDefinition.LEASE_SEED_CANNOT_REPEATED);
        Long id = seedDO.getId();
        if (id == null) {
            return "fail";
        }
        if (seedMapper.updateById(seedDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        Integer count = leaseSeedMapper.selectCount(new EntityWrapper<LeaseSeedDO>().eq("seed_id", id));
        if(count > 0)
            throw new AdminServiceException(ExceptionDefinition.LEASE_SEED_CANNOT_DELETE);
        if (seedMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    public SeedDO detail(Long id, Long adminId) throws ServiceException {
        return seedMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids))
            throw new AdminServiceException(ExceptionDefinition.LEASE_SEED_NOT_EXIST);
        Wrapper<LeaseSeedDO> wrapper = new EntityWrapper<LeaseSeedDO>().in("id", ids);
        if (leaseSeedMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.LEASE_SEED_CANNOT_DELETE);
        if (seedMapper.delete(new EntityWrapper<SeedDO>().in("id", ids)) <= 0)
            throw new AdminServiceException(ExceptionDefinition.NEWS_NOT_EXIST);
        return "ok";
    }

    @Override
    public List<SeedDO> select() throws ServiceException {
        return seedMapper.selectList(new EntityWrapper<SeedDO>().setSqlSelect("id", "`name`"));
    }

}
